//
//  XmlParserMarkedListInCategory.m
//  FleaFinder
//
//  Created by Hieu Le on 4/9/13.
//  Copyright (c) 2013 Kiwiweb. All rights reserved.
//

#import "XmlParserMarkedListInCategory.h"
#import "Marked.h"

@implementation XmlParserMarkedListInCategory

-(id)init
{
    self = [super init];
    
    if(self != nil){
        tagMarketType = @"markedtype";
        attrName = @"name";
        inSortedManner = NO;
        
        if(!appDelegate){
            appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        }
    }
    
    return self;
}

-(NSMutableArray*) getParsedObjects
{
    return markedArray;
}


-(void) parseXML: (NSString*) sourceUrl putIntoArray:(NSMutableArray*) destArr andOrganizedData:(NSMutableDictionary*)destOrganizedData inSortedManner:(BOOL)sortedManner
{
    markedArray = destArr;
    organizedData = destOrganizedData;
    inSortedManner = sortedManner;
    
    NSURL *url = [[NSURL alloc] initWithString: sourceUrl];
     
    NSError *error;
    NSString * dataString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
    
    //NSLog(@"%@", dataString);
    
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
    [xmlParser setDelegate:self];
    [xmlParser parse];
}


#pragma implementation of NSXMLParserDelegate

-(void) parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    //NSLog(@"Error = %@", parseError);
}

-(void) parserDidStartDocument:(NSXMLParser *)parser
{
    //NSLog(@"Start the document");
}

-(void) parserDidEndDocument:(NSXMLParser *)parser
{
    //NSLog(@"End the document");
}

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if([tagMarketType compare:elementName] == NSOrderedSame)
    {
        if(!currentMarketType)
        {
            currentMarketType = [[MarketType alloc] init];
            currentMarketType.name = [attributeDict objectForKey:attrName];
            currentTag = MARKET_TAG_MARKET_TYPE;
        }
    }

    [super parser:parser didStartElement:elementName namespaceURI:namespaceURI qualifiedName:qName attributes:attributeDict];
}

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([tagMarked compare:elementName] == NSOrderedSame)
    {
        if(currentMarked)
        {
            currentMarked.marketType = currentMarketType;
            [markedArray addObject:currentMarked];
            
            if(organizedData){
                if(!inSortedManner){
                    [((NSMutableArray*)[organizedData objectForKey:currentMarked.type]) addObject:currentMarked];
                } else {
                    [appDelegate addMarketToMarketListInSortedManner:currentMarked toList:[organizedData objectForKey:currentMarked.type]];
                }
            }
        }
        currentMarked = nil;
    }
    [super parser:parser didEndElement:elementName namespaceURI:namespaceURI qualifiedName:qName];
}

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [super parser:parser foundCharacters:string];
}


@end
